import path from 'node:path'
import process from 'node:process'

import vue from '@vitejs/plugin-vue'

import Unocss from 'unocss/vite'
import AutoImport from 'unplugin-auto-import/vite'

import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import Components from 'unplugin-vue-components/vite'
import { defineConfig, loadEnv } from 'vite'

import DevTools from 'vite-plugin-vue-devtools'

const pathSrc = path.resolve(__dirname, 'src')

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd())

  // 第5个参数是页面目录
  const argv = process.argv.slice(6)
  const pageDir = argv[0] || 'aaaaa'
  console.log('----------------------------')
  console.log('---- [dashboard]:', pageDir, '----')
  console.log('----------------------------')
  return {
    base: env.VITE_APP_BASE_PATH,
    define: {
      __PAGE_DIR__: JSON.stringify(pageDir),
    },
    resolve: {
      alias: {
        '~/': `${pathSrc}/`,
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          api: 'modern-compiler',
          additionalData: `@use "~/styles/element/index.scss" as *;`,
        },
      },
    },
    plugins: [
      vue(),
      AutoImport({
        imports: ['vue', 'vue-router', 'pinia'],
        dts: 'src/auto-import.d.ts',
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        // allow auto load markdown components under `./src/components/`
        extensions: ['vue', 'md'],
        // allow auto import and register components used in markdown
        include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
        resolvers: [
          ElementPlusResolver({
            importStyle: 'sass',
          }),
        ],
        dts: 'src/components.d.ts',
      }),

      // https://github.com/antfu/unocss
      // see unocss.config.ts for config
      Unocss(),

      DevTools(),
    ],
    server: {
      host: '0.0.0.0',
      proxy: {
        '/hls': {
          target: 'http://121.43.57.185',
          changeOrigin: true,
        },
        '/basic': {
          target: 'http://121.43.57.185:8093',
          changeOrigin: true,
          rewrite: path => path.replace(/^\/basic/, ''),
        },
        '/energy': {
          target: 'http://121.43.57.185:8092',
          changeOrigin: true,
          rewrite: path => path.replace(/^\/energy/, ''),
        },
        '/systemintegration': {
          target: 'http://121.43.57.185:8090',
          changeOrigin: true,
          rewrite: path => path.replace(/^\/systemintegration/, ''),
        },
      },
    },
  }
})
